package com.xl.rentkeeper.act;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.xl.fuse.protos.account.Ret;
import com.xl.fuse.protos.account.User;
import com.xl.rentkeeper.App;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.business.UserLogic;
import com.xl.rentkeeper.util.DialogUtil;
import com.xl.rentkeeper.util.ErrString;
import com.xl.rentkeeper.view.TitleBar;

/**
 * Created by Administrator on 2015/7/20.
 */
public class RegisterNo2Act extends BaseAct implements View.OnClickListener,Handler.Callback {
    private TextView mTVNext;
    private TextView mTVPhone;
    private TextView mTVResend;
    private Handler mHandler = new Handler(this);
    private int time ;
    private EditText mETVercode;
    private User mUser;
    private Dialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_register_no2);
        mUser = (User) getIntent().getSerializableExtra("user");
        //initToolBar();
        TitleBar mTitleBar = (TitleBar) findViewById(R.id.regiNo2_tl);
        initTitleBar(mTitleBar);
        initView();
        initResendTime();
    }
    private void sendVerCode(){
        if(TextUtils.isEmpty(mUser.phone) || mUser.phone.length()<11){
            App.showToast(ErrString.getErrTip(Ret.PHONE_FORMAT_ERR.getValue()));
            return ;
        }
        mDialog = DialogUtil.showProgressDialog(this,R.string.sending);
        hideSoftInput(mETVercode);
        UserLogic.getInstance().getVerCode(mUser);
    }
    private void checkVerCode(){
        String code = mETVercode.getText().toString().trim();
        if(TextUtils.isEmpty(code)){
            App.showToast(R.string.vercodeempty);
            return ;
        }
        mDialog = DialogUtil.showProgressDialog(this,R.string.loading);
        hideSoftInput(mETVercode);
        UserLogic.getInstance().checkVerCode(mUser,code);
    }
    void initResendTime(){
        mHandler.removeMessages(1);
        time=60;
        refreshTime();
        mTVResend.setEnabled(false);
        mHandler.sendEmptyMessageDelayed(1, 1000);

    }
    public String[] monitorEvents() {
        return new String[]{CMD_USER_GET_CODE,CMD_USER_CHECK_CODE};
    }
    void refreshTime(){
        mTVResend.setText(getResources().getString(R.string.resendvercode)+"("+time+")");
    }
    private void initView(){
        mTVPhone = (TextView) findViewById(R.id.tv_phone);
        mTVPhone.setText(mUser.phone);
        mTVNext = (TextView) findViewById(R.id.tv_next);
        mTVNext.setOnClickListener(this);
        mTVResend = (TextView) findViewById(R.id.tv_resend);
        mTVResend.setOnClickListener(this);
        mETVercode = (EditText) findViewById(R.id.et_vercode);
    }

    @Override
    public void onClick(View v) {
        if (v == mTVNext){
            checkVerCode();
        }else if(v == mTVResend){
            sendVerCode();
//            initResendTime();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeMessages(1);
    }

    @Override
    public boolean handleMessage(Message msg) {
        if(msg.what ==1){
           if(--time == 0){
               mTVResend.setText(getResources().getString(R.string.resendvercode));
               mTVResend.setEnabled(true);
               mHandler.removeMessages(1);
           }else{
               refreshTime();
               mTVResend.setEnabled(false);
               mHandler.sendEmptyMessageDelayed(1, 1000);
           }
        }
        return false;
    }
    @Override
    public void onEvent(String event, boolean ret, String errInfo, Object[] data) {
        super.onEvent(event, ret,errInfo, data);
        if (CMD_USER_GET_CODE.equals(event)) {
            if(mDialog!=null && mDialog.isShowing())mDialog.dismiss();
            if(ret){
                initResendTime();
            }
        }else if (CMD_USER_CHECK_CODE.equals(event)){
            if(mDialog!=null && mDialog.isShowing())mDialog.dismiss();
//            if(ret) {
                Intent intent = new Intent(this, RegisterNo3Act.class);
                intent.putExtra("user",mUser);
                startActivity(intent);
                finish();
//            }else{
//                Packet p = (Packet) data[0];
//                DialogUtil.showErrInfoDialog(this,ErrString.getErrTip(p.ret));
//            }

        }
    }
    @Override
    public boolean onBack(int keyCode, KeyEvent event) {
        DialogUtil.showBackDialog(this, R.string.tips, R.string.tips_back, R.string.sure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        },R.string.cancel,null).show();
        return false;
    }
}
